package com.atguigu.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * status: 0,   // 状态
         * wareId: 1,   // 仓库id
         */
        String status = (String) params.get("status");
        String wareId = (String) params.get("wareId");
        String key = (String) params.get("key");
        LambdaQueryWrapper<PurchaseDetailEntity> wrapper = Wrappers.<PurchaseDetailEntity>lambdaQuery()
                .eq(StringUtils.isNotBlank(status), PurchaseDetailEntity::getStatus, status)
                .and(StringUtils.isNotBlank(wareId), x -> x.eq(PurchaseDetailEntity::getWareId, wareId))
                .and(StringUtils.isNotBlank(key), x -> x.eq(PurchaseDetailEntity::getPurchaseId, key)
                        .or().like(PurchaseDetailEntity::getSkuId, key));

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper);

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        return this.list(Wrappers.<PurchaseDetailEntity>lambdaQuery()
                .eq(PurchaseDetailEntity::getPurchaseId, id));
    }

}